package com.bw.day523.代码;

import cn.hutool.json.JSONUtil;
import org.apache.flume.Context;
import org.apache.flume.Event;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

/**
 * @author y3068
 * @Classname Interceptor
 * @Description TODO
 * @Date 2025/5/23 15:27
 * @Version V1.0
 */

public class ETLInterceptor implements org.apache.flume.interceptor.Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        String body = new String(event.getBody(), StandardCharsets.UTF_8);
        if (JSONUtil.isJson(body)) {
            return event;
        }
        return null;
    }

    @Override
    public List<Event> intercept(List<Event> list) {
        Iterator<Event> iterator = list.iterator();
        while (iterator.hasNext()) {
            Event next = iterator.next();
            if (intercept(next) == null) {
                iterator.remove();
            }
        }
        return list;
    }

    public static class Builder implements org.apache.flume.interceptor.Interceptor.Builder {
        @Override
        public org.apache.flume.interceptor.Interceptor build() {
            return new ETLInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
    @Override
    public void close() {

    }
}